package de.vocabulary.server;

import java.util.List;
import java.util.Random;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import de.vocabulary.shared.model.LanguageModule;
import de.vocabulary.shared.model.Vocable;

public class SessionDetails {
	
	private static SessionDetails sessionDetails;
	private VocableDatabaseWrapper vocableDatabase;
	
	private SessionDetails(){
		//load default LanguageModule 
		languageModule = LanguageModuleManager.getLanguageModuleManager().getLanguageModule();
		vocableDatabase = VocableDatabaseWrapper.getVocableDatabaseWrapper(); 
	}
	
	public static SessionDetails getSessionDetails(){
		if (sessionDetails == null) sessionDetails = new SessionDetails();
		return sessionDetails;
				
	}
	
	private String lesson;
	private int amountVocabulary;
	private LanguageModule languageModule;
	
	private Vocable currentVocable;
	
	private List<Vocable> loadedVocs;
	
	private int[] vocableOrder;
	private int counter = 0;
	
	
	
	public Vocable getFirstVocable() {
		try {
			loadedVocs = vocableDatabase.getVocabularyList(lesson);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//TODO test rounding;
		int repetitionFactor = amountVocabulary/loadedVocs.size()+1;
		vocableOrder = new int[repetitionFactor*loadedVocs.size()];
		for (int i=0; i<repetitionFactor*loadedVocs.size(); i++)
			vocableOrder[i]=i%loadedVocs.size();
		shuffleArray(vocableOrder);
		System.out.println(amountVocabulary+" vs. "+StringUtils.join(ArrayUtils.toObject(vocableOrder), ","));
		currentVocable = loadedVocs.get(vocableOrder[counter]);
		return currentVocable;
		
	}
	
	public void setFeedback(boolean correct, boolean ignore) {
		if (ignore) return;
		currentVocable.setAmountRepetitions(currentVocable.getAmountRepetitions()+1);
		if (correct)
			currentVocable.setCorrectAnswers(currentVocable.getCorrectAnswers()+1);
	}
	
	public Vocable getNextVocable() {
		counter++;
		if (counter>=amountVocabulary)return null;
		currentVocable = loadedVocs.get(vocableOrder[counter]);
		return currentVocable;
	}

	
	//--- Shuffle ---------------------
	
	public static void shuffleArray(int[] a) {
	    int n = a.length;
	    Random random = new Random();
	    random.nextInt();
	    for (int i = 0; i < n; i++) {
	      int change = i + random.nextInt(n - i);
	      swap(a, i, change);
	    }
	  }

	  private static void swap(int[] a, int i, int change) {
	    int helper = a[i];
	    a[i] = a[change];
	    a[change] = helper;
	  }
	
	
	//--- getter / setter --------------
	
	
	public int getAmountVocabulary() {
		return amountVocabulary;
	}

	public void setAmountVocabulary(int amountVocabulary) {
		this.amountVocabulary = amountVocabulary;
	}

	public LanguageModule getLanguageModule() {
		return languageModule;
	}

	public void setLanguageModule(LanguageModule languageModule) {
		this.languageModule = languageModule;
	}

	public String getLesson() {
		return lesson;
	}

	public static void setSessionDetails(SessionDetails sessionDetails) {
		SessionDetails.sessionDetails = sessionDetails;
	}

	
	public void setLesson(String lesson){
		this.lesson = lesson;
	}


	

	
}
